<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addTodo="addTodo"></Header>
      <List :todoList="todoList"
            :changeDone="changeDone"
            :delTodo="delTodo"
      />
      <Footer :todoList="todoList"
              :delALlTodo="delALlTodo"
      ></Footer>
    </div>
  </div>
</template>

<script lang="ts">
import {defineComponent} from 'vue';

export default defineComponent({
  name: "App",

})

</script>

<script lang="ts" setup>

import Header from "./components/Header.vue";
import List from "./components/List.vue";
import Footer from "./components/Footer.vue";
import {ref} from "vue";
import {nanoid} from "nanoid";
import {TodosType, TodoType} from "./modules/TodoList";
const todoListRef = JSON.parse(localStorage.getItem("todoList") as string) || []
// 数据源
const todoList = ref<TodosType>(todoListRef)

// 添加
const addTodo = (newTodo: TodoType) => {
  todoList.value.unshift(newTodo);
  localStorage.setItem("todoList",JSON.stringify(todoList.value))
};

// 修改单个状态
const  changeDone = (id:string)=>{
  todoList.value.forEach((item)=>{
    item.done = item.id === id ? !item.done : item.done
  })
  localStorage.setItem("todoList",JSON.stringify(todoList.value))
}

// 删除单个todo
const delTodo = (id:string)=>{
  todoList.value = todoList.value.filter((item)=>item.id !== id)
  localStorage.setItem("todoList",JSON.stringify(todoList.value))
}

// 删除已完成
const delALlTodo = ()=>{
  todoList.value = todoList.value.filter(item=>!item.done)
  localStorage.setItem("todoList",JSON.stringify(todoList.value))
}

</script>

<style lang="scss" scoped>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
  0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>